﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class VendorInvoiceList : System.Web.UI.Page
    {
        VendorInvoiceController vendorInvoiceController=
            new VendorInvoiceController();
        Helper helper = new Helper(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["FlashMessage"] != null)
                {
                    Session["FlashMessage"] = Session["FlashMessage"].ToString();
                    Session["FlashMessageType"] = Session["FlashMessageType"].ToString();
                }
                
                //Bind all vendor invoices to grid view by default

                IList<VendorInvoiceEntity> vendorInvoiceEntities =
                    vendorInvoiceController.FindAllVendorInvoices();
                BindGridViewData(vendorInvoiceEntities);


                //Bind all vendors to drop down
                VendorController vendorController = new VendorController();
                IList<VendorEntity> vendorEntities = vendorController.FindAllVendorEntities();
                this.VendorDropDownList.DataSource = vendorEntities;
                this.VendorDropDownList.DataValueField = "Id";
                this.VendorDropDownList.DataTextField = "ShortName";
                this.VendorDropDownList.DataBind();

            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        private void BindGridViewData(IList<VendorInvoiceEntity> vendorInvoiceEntities)
        {
            this.VendorInvoiceListGridView.DataSource = vendorInvoiceEntities;
            this.VendorInvoiceListGridView.DataBind();
        }


        protected void DateSearchButton_Click(object sender, EventArgs e)
        {
            DateTime fromDate = Convert.ToDateTime(FromTextBox.Text.Trim());
            DateTime toDate = Convert.ToDateTime(ToTextBox.Text.Trim());
            IList<VendorInvoiceEntity> vendorInvoiceEntities =
                vendorInvoiceController.FindVendorInvoiceByDate(fromDate, toDate);
            BindGridViewData(vendorInvoiceEntities);
        }

        protected void InvoiceNoSearchButton_Click(object sender, EventArgs e)
        {
            string invoiceNo = InvoiceNoTextBox.Text.Trim().ToUpper();
            IList<VendorInvoiceEntity> vendorInvoiceEntities =
                vendorInvoiceController.FindVendorInvoiceByInvoiceNo(invoiceNo);
            BindGridViewData(vendorInvoiceEntities);
        }

        protected void VendorSearchButton_Click(object sender, EventArgs e)
        {
            int vendorId = Convert.ToInt32(VendorDropDownList.SelectedValue);
            IList<VendorInvoiceEntity> vendorInvoiceEntities =
                vendorInvoiceController.FindVendorInvoiceByVendor(vendorId);
            BindGridViewData(vendorInvoiceEntities);
        }

        protected void VendorInvoiceListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            VendorInvoiceListGridView.PageIndex = e.NewPageIndex;
            IList<VendorInvoiceEntity> vendorInvoiceEntities =
                    vendorInvoiceController.FindAllVendorInvoices();
            BindGridViewData(vendorInvoiceEntities);
        }
    }
}